package com.unisound.tracelog.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TimeUtils {

    //检查日期中月份和天数合理性
    public static boolean check_date(String month, String day) {
        try {
            int month_int = Integer.parseInt(month);
            if (month_int < 1 || month_int > 12) {
                return false;
            }
            int day_int = Integer.parseInt(day);
            return day_int >= 1 && day_int <= 31;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @param timeStr 日期字符串
     * @return (年, 月 - 日)
     */
    public static Pair<String, String> parseYearAndDate(String timeStr) {
        String year = "";
        String date = "";
        Pattern pattern = Pattern.compile("(\\d{4})[-/](\\d{1,2}[-/]\\d{1,2})");
        Matcher matcher = pattern.matcher(timeStr);
        if (matcher.find()) {
            year = matcher.group(1);
            String[] items = matcher.group(2).split("[-/]");
            for (int i = 0; i < items.length; i++) {
                if (items[i].length() == 1) {
                    items[i] = "0" + items[i];
                }
            }
            date = String.join("-", items);
            return Pair.of(year, date);
        }
        pattern = Pattern.compile("(\\d{4})年(\\d{1,2})月(\\d{1,2})日");
        matcher = pattern.matcher(timeStr);
        if (matcher.find()) {
            year = matcher.group(1);
            String month = matcher.group(2);
            if (month.length() == 1) {
                month = "0" + month;
            }
            String day = matcher.group(3);
            if (day.length() == 1) {
                day = "0" + day;
            }
            date = month + "-" + day;
            return Pair.of(year, date);
        }
        pattern = Pattern.compile("(\\d{1,2}[-/]\\d{1,2})[-/](\\d{4})");
        matcher = pattern.matcher(timeStr);
        if (matcher.find()) {
            year = matcher.group(2);
            String[] items = matcher.group(1).split("[-/]");
            List<String> dateList = new ArrayList<>();
            for (int i = items.length - 1; i >= 0; i--) {
                if (items[i].length() == 1) {
                    dateList.add("0" + items[i]);
                } else {
                    dateList.add(items[i]);
                }
            }
            date = String.join("-", dateList);
            return Pair.of(year, date);
        }
        pattern = Pattern.compile("20\\d{2}");
        matcher = pattern.matcher(timeStr);
        if (matcher.find()) {
            year = matcher.group(0);
        }
        timeStr = timeStr.replace(year, "");
        pattern = Pattern.compile("\\d{1,2}[-/]\\d{1,2}");
        matcher = pattern.matcher(timeStr);
        if (matcher.find()) {
            boolean flag = true;
            String[] items = matcher.group(0).split("[-/]");
            for (int i = 0; i < items.length; i++) {
                if ((i == 0 && Integer.parseInt(items[i]) > 12) || (i == 1 && Integer.parseInt(items[i]) > 31)) {
                    flag = false;
                    break;
                }
                if (items[i].length() == 1) {
                    items[i] = "0" + items[i];
                }
            }
            if (flag) {
                date = String.join("-", items);
            } else {
                date = "";
            }
        }
        return Pair.of(year, date);
    }

    /**
     * @param timeStr 时间字符串
     * @return 时:分:秒
     */
    public static String parseTime(String timeStr) {
        Pattern pattern = Pattern.compile("\\d{1,2}[:：.]\\d{1,2}[:：.]\\d{1,2}");
        Matcher matcher = pattern.matcher(timeStr);
        if (matcher.find()) {
            String[] items = matcher.group(0).split("[:：.]");
            for (int i = 0; i < items.length; i++) {
                if (items[i].length() == 1) {
                    items[i] = "0" + items[i];
                }
            }
            return String.join(":", items);
        }
        pattern = Pattern.compile("\\d{1,2}[:：.]\\d{1,2}");
        matcher = pattern.matcher(timeStr);
        if (matcher.find()) {
            String[] items = matcher.group(0).split("[:：.]");
            for (int i = 0; i < items.length; i++) {
                if (items[i].length() == 1) {
                    items[i] = "0" + items[i];
                }
            }
            return String.join(":", items) + ":00";
        }
        pattern = Pattern.compile("(\\d{1,2})时((\\d{1,2})分)?((\\d{1,2})秒)?");
        matcher = pattern.matcher(timeStr);
        if (matcher.find()) {
            String hour = matcher.group(1);
            if (hour.length() == 1) {
                hour = "0" + hour;
            }
            String minute = matcher.group(3);
            if (minute == null) {
                minute = "00";
            } else if (minute.length() == 1) {
                minute = "0" + minute;
            }
            String second = matcher.group(5);
            if (second == null) {
                second = "00";
            } else if (second.length() == 1) {
                second = "0" + second;
            }
            return hour + ":" + minute + ":" + second;
        }
        return "";
    }

    public static String parseDate(String value) {
        Pair<String, String> yearAndDate = TimeUtils.parseYearAndDate(value);
        String year = yearAndDate.getLeft();
        String date = yearAndDate.getRight();

        String time = TimeUtils.parseTime(value);
        if (StringUtils.isNotBlank(year) && StringUtils.isNotBlank(date)) {
            if (StringUtils.isEmpty(time)) {
                time = "00:00:00";
            }
            value = year + "-" + date + " " + time;
        } else {
            value = "";
        }
        return value;
    }
}
